package co.edu.unal.satacredito.rest.auth;

import javax.ws.rs.GET;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.Context;
import javax.ws.rs.core.MediaType;

import com.sun.jersey.api.representation.Form;
import com.sun.jersey.api.view.Viewable;
import com.sun.jersey.oauth.server.api.providers.DefaultOAuthProvider;
import com.sun.jersey.oauth.server.api.providers.DefaultOAuthProvider.Consumer;

/**
 * Root resource we are using for redirecting to the user-specific URL
 */
@Path("/register")
public class RegisterResource extends ResourceBase {
	
	private @Context DefaultOAuthProvider provider;
	
	@Path("register")
    @GET
    @Produces(MediaType.TEXT_HTML)
    public Viewable getHtml() {
        // make sure the right user is logged in
//        checkUser();
        return new Viewable("/consumerregistration", null);
    }
	
	@Path("register")
	@POST
    @Produces(MediaType.TEXT_HTML)
    public Viewable post(Form params) {
        // make sure the right user is logged in
//        checkUser();
        // only allow if the user has write access
//        checkWrite();
        // register a new consumer
        Consumer consumer = provider.registerConsumer(user, params);
        // redirect to the consumer detail
        
        System.out.println("Principal 1 "+consumer.getPrincipal());
        
        Consumer consumer2 = provider.getConsumer(consumer.getKey());
        
        System.out.println("Principal 2 "+consumer2.getPrincipal());
        
        
        return new Viewable("/consumer", consumer2);
    }
	
//	@Path("{consumerKey}")
//    @GET
//    @Produces(MediaType.TEXT_HTML)
//    public Viewable getConsumer(@PathParam("consumerKey") String consumerKey) {
//        checkUser();
//        // retrieve the consumer, check the owner and pass it to the viewable
//        DefaultOAuthProvider.Consumer c = provider.getConsumer(consumerKey);
//        if (!user.equals(c.getOwner())) throw new NotFoundException();
//        return new Viewable("/consumer", c);
//    }
}
